﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.BLL;
using Grove.ORM;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;

public partial class Agency_Check_BidGood : System.Web.UI.Page
{
    public string BId;
    string ProjectId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
         
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        ProjectId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["projectid"]));
        
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (!IsPostBack)
            {
                Db db = new Db();
                string strSql = "select distinct AccountId_TB, salername from bidgoods_view where projectid=" + ProjectId + " order by salername";
                System.Data.IDataReader dr = db.DataReader(strSql);
                Company.DataSource = dr;
                Company.DataTextField = "salername";
                Company.DataValueField = "AccountId_TB";
                Company.DataBind();
                Company.Items.Insert(0, new ListItem("全部", "-1"));
                dr.Close();
                dr.Dispose();
                db.Close();
                ProductCode.Text = "";
                ProductName.Text = "";
                Outlookc.Text = "";
                Company.SelectedValue = "";
                lstCheckStatus.Items.Add("全部");
                lstCheckStatus.Items.Add("未审核");
                lstCheckStatus.Items.Add("审核通过");
                lstCheckStatus.Items.Add("审核未通过");
                lstMemo3.Items.Add("全部");
                lstMemo3.Items.Add("已保存未提交");
                lstMemo3.Items.Add("已提交");
                lstMemo3.Items.Add("已修改");

                showddltbshr();
                Session["IsCheck"]="1";
            }
            if (Request.Form["chk"] != null)
            {
                string _strBidId = Request.Form["chk"].ToString(), _strSub, strSql;
                _strSub = Request.Form["subPass"];
                BId = Request.Form["chk"].ToString();
                if (_strSub == "" || _strSub == null)
                {
                    _strSub = Request.Form["subNoPass"];
                }
                Db db1 = new Db();
                string atname = _Account.AccountName;
                if (_strSub=="审核通过")
                {
                        strSql = "update project_bid set checkstatus=1,memo8='" + atname + "',SHSJ=getdate(),memo9=null where bidid in (" + _strBidId + ")";
                        db1.Command(strSql);
                        //HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "project_bid", strSql, "../Agency/Check_BidGood.aspx");

                }
                else if ((_strSub == "审核不通过") && (Session["IsCheck"].ToString()=="1"))
                {
                    Response.Write("<Script>window.open('BidNoPass.aspx?Bidid=" + BId + "')</Script>");
                }
                db1.Close();
            }
            Session["IsCheck"] = "1";
            showGridView();
        }
    }

    public void showddltbshr()
    {
        Db db = new Db();
        System.Data.IDataReader dr;
        string sqltbr = "Select AccountName from account where type=7";
        dr=db.DataReader(sqltbr);
        ddl_tbshr.DataSource = dr;
        ddl_tbshr.DataTextField = "AccountName";
        ddl_tbshr.DataValueField = "AccountName";
        ddl_tbshr.DataBind();
        ddl_tbshr.Items.Insert(0, new ListItem("全部", "全部"));
        dr.Close();
        dr.Dispose();
        db.Close();
    }

    public void showGridView()
    {
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "bidgoods_view";                                               //要查询的表明
        dbPage.Key = "bidid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "MEMO8,factoryusercode,MEMO9,bidid,goodsid,ProductName,Outlookc,UserCode_TB,Factor,Medicinemodel,Unit,ProductCode,salername,factoryname,goodsName,ProductNameSM,case  when CheckStatus is   null then '未审核' when CheckStatus='0' then '未审核'  when CheckStatus='1' then '审核通过' when CheckStatus='2' then '不通过'   when CheckStatus='3' then '废标' when CheckStatus='4' then '超过限价废标' when CheckStatus='5' then '竞价废标' else '未知' end  as CheckStatus,case  when Company_SC_check is   null then '未审核' when Company_SC_check='0' then '未审核'  when Company_SC_check='1' then '审核通过' when Company_SC_check='2' then '不通过' end as Company_SC_check ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "factoryusercode,productname,medicinemodel,outlookc,bidid";//排序字段


        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td class='table_title' nowrap>流水号</td>");
        strHtml.Append("<td class='table_title' nowrap>注册号</td>");
        strHtml.Append("<td class='table_title' nowrap>投标人</td>");
        strHtml.Append("<td class='table_title' nowrap>品目号</td>");
        strHtml.Append("<td class='table_title' nowrap>通用名</td>");
        strHtml.Append("<td class='table_title' nowrap>说明书通用名</td>");
        strHtml.Append("<td class='table_title' nowrap>规格</td>");
        strHtml.Append("<td class='table_title' nowrap>剂型</td>");
        strHtml.Append("<td class='table_title' nowrap>转换系数</td>");
        strHtml.Append("<td class='table_title' nowrap>生产厂家</td>");
        strHtml.Append("<td class='table_title' nowrap>注册号</td>");
        strHtml.Append("<td class='table_title' nowrap>审核状态</td>");
        strHtml.Append("<td class='table_title' nowrap>审核人</td>");
        strHtml.Append("<td class='table_title' nowrap>查看</td>");
        strHtml.Append("<td class='table_title' nowrap>选择</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();

        while (dr.Read())
        {
            string _strCheckStatus = dr["CheckStatus"].ToString();
            if (_strCheckStatus == "不通过")
            {
                strHtml.Append("<tr style='background-color:Yellow'>"); 
            }
            else
            {
                if (i == 0)
                {
                    strHtml.Append("<tr class='table_td'>");
                    i++;
                }
                else
                {
                    strHtml.Append("<tr class='table_td2'>");
                    i--;
                }
            }
           
            strHtml.Append("<td>" + dr["goodsid"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode_TB"].ToString() + "</td>");
            strHtml.Append("<td><input type='hidden' name='AccountId' value='" + dr["bidid"].ToString() + "'>" + dr["salername"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductCode"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductName"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductNameSM"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Outlookc"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Medicinemodel"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Factor"].ToString() + "</td>");
            if (dr["Company_SC_check"].ToString() == "审核通过")
            {
                strHtml.Append("<td title='" + dr["Company_SC_check"].ToString() + "' style=' color: #00cc66;' >" + dr["factoryname"].ToString() + "</td>");
            }
            else if (dr["Company_SC_check"].ToString() == "不通过")
            {
                strHtml.Append("<td title='" + dr["Company_SC_check"].ToString() + "' style=' color: #ff0033;'>" + dr["factoryname"].ToString() + "</td>");
            }
            else
            {
                strHtml.Append("<td title='" + dr["Company_SC_check"].ToString() + "'>" + dr["factoryname"].ToString() + "</td>");
            }
            strHtml.Append("<td>" + dr["factoryusercode"].ToString() + "</td>");
            strHtml.Append("<td title='" + dr["MEMO9"].ToString() + "'>" + _strCheckStatus + "</td>");
            strHtml.Append("<td>" + dr["MEMO8"].ToString() + "</td>");//审核人

            strHtml.Append("<td><a target=_blank href='BidGoods_detail.aspx?BidId=" + dr["BidId"].ToString() + "&CheckStatus=" + dr["CheckStatus"].ToString() + "&ProjectId=" + ProjectId + "&Id=2'>查看</a></td>");
            strHtml.Append("<td><input name='chk' type='checkbox' value='" + dr["bidid"].ToString() + "'></td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();

        //BId = Request.Form["bidid"].ToString();
        //GridView1.DataSource = dr;
        //GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键




    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp, CheckStatus, _SC, _GoodsId, name, outlookc, company, productcode,CompanyUserCode,tbr,scqyzch;
        productcode = ProductCode.Text.ToString().Trim();
        name = ProductName.Text.ToString().Trim();
        outlookc = Outlookc.Text.ToString().Trim();
        company = SysFun.ToTrim(Company.SelectedValue);
        CompanyUserCode = txtCompanyUserCode.Text.ToString().Trim();
        string ProjectId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["projectid"]));
        //projectid = listProject.SelectedValue;
        strtemp = " bidgoods_view.projectid=" + ProjectId + "  and";
        CheckStatus = lstCheckStatus.SelectedValue;
        _SC = SysFun.ToTrim(txtSC.Text);
        _GoodsId = SysFun.ToTrim(txtGoodsId.Text);
        scqyzch = SysFun.ToTrim(txtscqyzch.Text);
        string strProductNameSM = ProductNameSM.Text.ToString().Trim();
        string strMedicinemodel = Medicinemodel.Text.ToString().Trim();
        string strFactor = Factor.Text.ToString().Trim();
        switch (CheckStatus)
        {
            case "全部":
                if (chk1.Checked == true)
                {
                    strtemp += " productname+convert(varchar(255),accountid_sc) in (select productname+convert(varchar(255),goods.accountid_sc) as ProductNameAndAccountId_SC from product ,goods,project_bid where product.productid=goods.productid and goods.goodsid=project_bid.goodsid and projectid=" + ProjectId + "  group by productname+convert(varchar(255),goods.accountid_sc) having max(project_bid.Accountid)<>min(project_bid.Accountid)) and";
                }
                break;
            case "未审核":
                strtemp += " (CheckStatus=0 or CheckStatus is null) and";
                if (chk1.Checked == true)
                {
                    strtemp += " productname+convert(varchar(255),accountid_sc) in (select productname+convert(varchar(255),goods.accountid_sc) as ProductNameAndAccountId_SC from product ,goods,project_bid where product.productid=goods.productid and goods.goodsid=project_bid.goodsid  and (project_bid.CheckStatus=0 or project_bid.CheckStatus is null) and projectid=" + ProjectId + "  group by productname+convert(varchar(255),goods.accountid_sc) having max(project_bid.Accountid)<>min(project_bid.Accountid)) and";
                }
                break;
            case "审核通过":
                strtemp += " CheckStatus=1 and";
                if (chk1.Checked == true)
                {
                    strtemp += " productname+convert(varchar(255),accountid_sc) in (select productname+convert(varchar(255),goods.accountid_sc) as ProductNameAndAccountId_SC from product ,goods,project_bid where product.productid=goods.productid and goods.goodsid=project_bid.goodsid and  project_bid.CheckStatus=1 and projectid=" + ProjectId + "  group by productname+convert(varchar(255),goods.accountid_sc) having max(project_bid.Accountid)<>min(project_bid.Accountid)) and";
                }
                break;
            case "审核未通过":
                strtemp += " CheckStatus=2 and";
                if (chk1.Checked == true)
                {
                    strtemp += " productname+convert(varchar(255),accountid_sc) in (select productname+convert(varchar(255),goods.accountid_sc) as ProductNameAndAccountId_SC from product ,goods,project_bid where product.productid=goods.productid and goods.goodsid=project_bid.goodsid and  project_bid.CheckStatus=2 and projectid=" + ProjectId + "  group by productname+convert(varchar(255),goods.accountid_sc) having max(project_bid.Accountid)<>min(project_bid.Accountid)) and";
                }
                break;
        }
        //switch (CheckStatus)
        //{
        //    case "全部":
        //        if (chk1.Checked == true)
        //        {
        //            strtemp += " goodsid in  (select goodsid from project_bid where projectid=" + ProjectId + "   group by  goodsid having count(*)>1 ) and";
        //        }
        //        break;
        //    case "未审核":
        //        strtemp += " (CheckStatus=0 or CheckStatus is null) and";
        //        if (chk1.Checked == true)
        //        {
        //            strtemp += " goodsid in  (select goodsid from project_bid  where projectid=" + ProjectId + " and  (CheckStatus=0 or CheckStatus is null)   group by  goodsid having count(*)>1 ) and";
        //        }
        //        break;
        //    case "审核通过":
        //        strtemp += " CheckStatus=1 and";
        //        if (chk1.Checked == true)
        //        {
        //            strtemp += " goodsid in  (select goodsid from project_bid  where projectid=" + ProjectId + " and  CheckStatus=1  group by  goodsid having count(*)>1 ) and";
        //        }
        //        break;
        //    case "审核未通过":
        //        strtemp += " CheckStatus=2 and";
        //        if (chk1.Checked == true)
        //        {
        //            strtemp += " goodsid in  (select goodsid from project_bid  where projectid=" + ProjectId + " and  CheckStatus=2  group by  goodsid having count(*)>1 ) and";
        //        }
        //        break;
        //}
        //提交状态

        string _Memo3 = lstMemo3.SelectedValue;
        switch (_Memo3)
        {
            case "全部":
                break;
            case "已保存未提交":
                strtemp += " Memo3=0 and";
                break;
            case "已提交":
                strtemp += " Memo3=1 and";
                break;
            case "已修改":
                strtemp += " Memo3=2 and";
                break;
        }

        tbr = ddl_tbshr.SelectedValue;
        if (tbr == "全部" || tbr == "")  //按审核人查询
        {
        }
        else
        {
            strtemp += " MEMO8='" + tbr + "' And"; 
        }

        if (scqyzch != "") //按生产企业注册号查询
        {
            strtemp += " factoryusercode like '%"+ scqyzch +"%' and";
        }
        

        if (_GoodsId != "")
        {
            strtemp += " GoodsId like '%" + _GoodsId + "%' and";

        }
        if (_SC != "")
        {
            strtemp += " factoryname like '%" + _SC + "%' and";

        }
        if (productcode != "")
        {
            strtemp += " productcode like '%" + productcode + "%' and";

        }
        if (name != "")
        {
            strtemp += " (ProductName like '%" + name + "%'or SpellingCode like '%"+ name + "%') and";
        }
        if (outlookc != "")
        {
            strtemp += " outlookc like '%" + outlookc + "%' and";
        }
        if (company != "-1")
        {
            strtemp += " AccountId_TB=" + company + " and";
        }
        if (CompanyUserCode != "") 
        {
            strtemp += " UserCode_TB='" + CompanyUserCode + "' and";
        }
        if (strProductNameSM != "")
        {
             strtemp += " ProductNameSM like '%" + strProductNameSM + "%' and";
        }
        if (strMedicinemodel != "")
        {
            strtemp += "  Medicinemodel like '%" + strMedicinemodel + "%' and";
        }
        if (strFactor != "")
        {
            strtemp += " Factor = '" + strFactor + "' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
     
        return strtemp;

    }



    protected void Button5_Click(object sender, EventArgs e)//查询
    {
        if (chk1.Checked)
        {
            buttonCheck.Visible = true;
        }
        else
        {
            buttonCheck.Visible = false;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)//全部审核通过
    {
        if (Request.Form["chk"] != null)
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            string _strBidId = Request.Form["chk"].ToString(), strSql;
            Db db = new Db();
            string atname = _Account.DWName;
            strSql = "update project_bid set CheckStatus='1',MEMO9='自行采购一过',SHSJ=getdate(),memo8='" + atname + "' where BidId in(" + _strBidId + ")";
            db.Command(strSql);
            strSql = "update goods set CheckStatus='1',shenhetime=getdate(),AccountId_SH=" + _Account.AccountId.ToString() + " where goodsid not in (select distinct goodsid from project_bid where projectid=" + ProjectId + " and goodsid in(select distinct goodsid from project_bid where bidid in(" + _strBidId + ") and checkstatus=1)) and goodsid in(select distinct goodsid from project_bid where bidid in(" + _strBidId + "))";
            db.Command(strSql);
            db.Close();
            Response.Write("<script>alert('审核通过！！！');window.opener.location.reload();window.close();</script>");
        }
        else
        {
            Response.Write("<script>alert('请选择商品！');</script>");
        }
    }
    protected void buttonCheck_Click(object sender, EventArgs e) //全部审核不 通过
    {
        if (Request.Form["chk"] != null)
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            string _strBidId = Request.Form["chk"].ToString(), strSql;
            Db db = new Db();
            string atname = _Account.DWName;
            strSql = "update project_bid set CheckStatus='2',MEMO9='一标多投',SHSJ=getdate(),memo8='" + atname + "' where BidId in(" + _strBidId + ")";
            db.Command(strSql);
            strSql = "update goods set CheckStatus='2',NoPass='一标多投',shenhetime=getdate(),AccountId_SH=" + _Account.AccountId.ToString() + " where goodsid not in (select distinct goodsid from project_bid where projectid=" + ProjectId + " and goodsid in(select distinct goodsid from project_bid where bidid in(" + _strBidId + ") and checkstatus=1)) and goodsid in(select distinct goodsid from project_bid where bidid in(" + _strBidId + "))";
            db.Command(strSql);
            db.Close();
            Response.Write("<script>alert('审核不通过！！！');window.opener.location.reload();window.close();</script>");
        }
        else
        {
            Response.Write("<script>alert('请选择商品！');</script>");
        }
       
    }
    protected void Button1_Click(object sender, EventArgs e)//导出excel
    {
        
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("投标商品审核一览表.xls"));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentEncoding = System.Text.Encoding.UTF7;  //解决将gridview导出成excel时出现乱码  
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        string strselet = "select  goodsid as 流水号,salername as 投标人,UserCode_TB as 注册号,ProductCode as 品目号,ProductName as 通用名,ProductNameSM as 说明书通用名,Outlookc as 规格,Medicinemodel as 剂型,Factor as 转换系数,factoryname as 生产厂家,factoryusercode as 注册号,case  when CheckStatus is   null then '未审核' when CheckStatus='0' then '未审核'  when CheckStatus='1' then '审核通过' when CheckStatus='2' then '不通过'   when CheckStatus='3' then '废标' when CheckStatus='4' then '超过限价废标' when CheckStatus='5' then '竞价废标' else '未知' end  as 审核状态,MEMO8 as 审核人 from bidgoods_view where " + fctConditions();
        Db db = new Db();
        GridView1.DataSource = db.DataTable(strselet);
        GridView1.DataBind();
        db.Close();
        this.GridView1.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行

       
              Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！

    }

   
}

